package de.playmee.services;

import de.playmee.MainActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public abstract class ExtendedService extends Service {
	
	private final String TAG = "ExtendedService";
	private Notification notification;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	/**
	 * configures service as a foreground service and appears to the user
	 * as a notification
	 * 
	 * @param notification
	 * @param notificationId
	 * @param text
	 * @param drawable
	 */
	protected void setUpAsForeground(int notificationId, String text, int drawable) {
		PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
				0, new Intent(getApplicationContext(), MainActivity.class),
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification = new Notification();
		notification.tickerText = text;
		notification.icon = drawable;
		notification.flags |= Notification.FLAG_ONGOING_EVENT;
		notification.setLatestEventInfo(getApplicationContext(), "playMEE", text, pi);
		startForeground(notificationId, notification);
	}
	
	/**
	 * updates the notification
	 * 
	 * @param text
	 */
	protected void updateNotification(int notificationId, String text) {
		if (notification == null) {
			Log.e(TAG, "Cannot update notification. It has to be created before it can be updated.");
			return;
		}
		PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
				0, new Intent(getApplicationContext(), MainActivity.class),
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification.setLatestEventInfo(getApplicationContext(), "playMEE", text, pi);
		NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		manager.notify(notificationId, notification);
	}
}
